﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.Electric;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP.Electric.Common;
using YArchitech.Revit.Electric.Common;
using HYPreviewerControl;

namespace HYRElectric.Equipment
{
	public partial class SwitchBoxesLayoutForm : System.Windows.Forms.Form
	{
		public SwitchBoxesLayoutForm()
		{
			this.InitializeComponent();
			//base.Icon = Resource.YArchitechIcon;
			this.ckbDisplayNum.Checked = true;
			this.cmbArrangementStyle.SelectedIndex = 0;
			this.m_MainPreviewer = new Previewer(1u);
			this.m_MainPreviewer.Name = "RvtCvsPreviewer";
            this.m_MainPreviewer.BackColor = System.Drawing.Color.White;
			this.m_MainPreviewer.BorderStyle = BorderStyle.FixedSingle;
			this.m_MainPreviewer.Location = this.panelPreview.Location;
			this.m_MainPreviewer.Size = this.panelPreview.Size;
			this.m_MainPreviewer.SizeMode = PictureBoxSizeMode.AutoSize;
			this.m_MainPreviewer.TabIndex = 9;
			this.m_MainPreviewer.TabStop = false;
			this.m_MainPreviewer.Paint += this.Previewer_Paint;
			this.m_MainPreviewer.ZoomExtents();
			base.Controls.Add(this.m_MainPreviewer);
		}

		private void InitBtn()
		{
			//base.Icon = Resource.YArchitechIcon;
			this.btnInsertPt.Image = YArchitech.Electric.Resources.GetImage("SwitchInsertPt16");
			this.btnRotation.Image = YArchitech.Electric.Resources.GetImage("SwitchArray16");
			ToolTip toolTip = new ToolTip();
			toolTip.InitialDelay = 500;
			toolTip.AutoPopDelay = 3000;
			toolTip.ReshowDelay = 50;
			toolTip.ShowAlways = true;
			toolTip.SetToolTip(this.btnInsertPt, "切换插入点");
			toolTip.SetToolTip(this.btnRotation, "切换排布方案");
			toolTip.SetToolTip(this.m_MainPreviewer, "仅表现排布预览角度和插入点位置,并非实物预览效果!");
		}

		private void UpdatePicShow()
		{
			this.m_MainPreviewer.Refresh();
		}

		private void SaveDlgData()
		{
			string workingLocation = Product.WorkingLocation;
			string xmlPath = workingLocation + "SwitchBoxesLayoutForm.xml";
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(workingLocation, "Root");
			xmlHelper.SelectNode("Root\\Datas");
			xmlHelper.SetValue("Num", this.txtNum.Value.ToString());
			xmlHelper.SetValue("DisplayNum", this.ckbDisplayNum.Checked.ToString());
			xmlHelper.SetValue("XOffset", this.txtXOffset.Text);
			xmlHelper.SetValue("YOffset", this.txtYOffset.Text);
			xmlHelper.SetValue("HeadNum", this.txtHeadNum.Text);
			xmlHelper.SetValue("StartNum", this.txtStartNum.Value.ToString());
			xmlHelper.SetValue("ArrangementStyle", this.cmbArrangementStyle.SelectedItem.ToString());
			xmlHelper.SetValue("EInsPos", Convert.ToString((int)this.m_nInsPos));
			xmlHelper.SetValue("EPlanAng", Convert.ToString((int)this.m_nPlanAng));
			xmlHelper.SetAttribute("X", base.Location.X.ToString());
			xmlHelper.SetAttribute("Y", base.Location.Y.ToString());
			xmlHelper.SaveXml(xmlPath);
		}

		private void LoadDlgData()
		{
			string text = Product.WorkingLocation + "SwitchBoxesLayoutForm.xml";
			if (!File.Exists(text))
			{
				return;
			}
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(text, "Root");
			xmlHelper.SelectNode("Root\\Datas");
			this.txtNum.Text = xmlHelper.GetValue("Num");
			if (xmlHelper.GetValue("DisplayNum") == "True")
			{
				this.ckbDisplayNum.Checked = true;
			}
			else
			{
				this.ckbDisplayNum.Checked = false;
			}
			this.txtXOffset.Text = xmlHelper.GetValueEx("XOffset", "0");
			this.txtYOffset.Text = xmlHelper.GetValueEx("YOffset", "0");
			this.txtHeadNum.Text = xmlHelper.GetValue("HeadNum");
			this.txtStartNum.Text = xmlHelper.GetValueEx("StartNum", "1");
			if (xmlHelper.GetValue("ArrangementStyle") == "升序")
			{
				this.cmbArrangementStyle.SelectedIndex = 0;
			}
			else
			{
				this.cmbArrangementStyle.SelectedIndex = 1;
			}
			this.m_nInsPos = (SwitchBoxesLayoutForm.EInsPos)Convert.ToInt32(xmlHelper.GetValueEx("EInsPos", "0"));
			this.m_nPlanAng = (SwitchBoxesLayoutForm.EPlanAng)Convert.ToInt32(xmlHelper.GetValueEx("EPlanAng", "0"));
			int x = Convert.ToInt32(xmlHelper.GetAttributeEx("X", "100"));
			int y = Convert.ToInt32(xmlHelper.GetAttributeEx("Y", "100"));
			base.Location = new System.Drawing.Point(x, y);
		}

		private void SwitchBoxesLayoutForm_Load(object sender, EventArgs e)
		{
			this.m_IsInitialized = false;
			this.LoadDlgData();
			this.InitBtn();
			this.m_IsInitialized = true;
			this.UpdatePicShow();
		}

		private void SwitchBoxesLayoutForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.SaveDlgData();
		}

		private void ckbDisplayNum_CheckedChanged(object sender, EventArgs e)
		{
			bool @checked = this.ckbDisplayNum.Checked;
			this.label3.Enabled = @checked;
			this.label4.Enabled = @checked;
			this.label5.Enabled = @checked;
			this.txtHeadNum.Enabled = @checked;
			this.txtStartNum.Enabled = @checked;
			this.cmbArrangementStyle.Enabled = @checked;
		}

		private void btnInsertPt_Click(object sender, EventArgs e)
		{
			this.m_nInsPos++;
			if (this.m_nInsPos == SwitchBoxesLayoutForm.EInsPos.eInsPosInvalid)
			{
				this.m_nInsPos = SwitchBoxesLayoutForm.EInsPos.eInsPosLeftUp;
			}
			this.UpdatePicShow();
			if (this.m_IsInitialized)
			{
				this.m_InfoChanged = true;
			}
		}

		private void btnRotation_Click(object sender, EventArgs e)
		{
			this.m_nPlanAng++;
			if (this.m_nPlanAng == SwitchBoxesLayoutForm.EPlanAng.ePlanAngInvalid)
			{
				this.m_nPlanAng = SwitchBoxesLayoutForm.EPlanAng.ePlanAng0;
			}
			this.UpdatePicShow();
			if (this.m_IsInitialized)
			{
				this.m_InfoChanged = true;
			}
		}

		private void txtXOffset_TextChanged(object sender, EventArgs e)
		{
			if (this.m_IsInitialized)
			{
				this.m_InfoChanged = true;
			}
		}

		private void txtYOffset_TextChanged(object sender, EventArgs e)
		{
			if (this.m_IsInitialized)
			{
				this.m_InfoChanged = true;
			}
		}

		private void txtNum_ValueChanged(object sender, EventArgs e)
		{
			if (this.m_IsInitialized)
			{
				this.m_InfoChanged = true;
			}
		}

		private void SwitchBoxesLayoutForm_Deactivate(object sender, EventArgs e)
		{
			string text = this.txtXOffset.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtXOffset.Text = this.txtXOffset.OldValue;
			}
			text = this.txtXOffset.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtXOffset.Text = "0";
			}
			text = this.txtYOffset.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtYOffset.Text = this.txtYOffset.OldValue;
			}
			text = this.txtYOffset.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtYOffset.Text = "0";
			}
			text = this.txtStartNum.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtStartNum.Text = "1";
			}
		}

		private void SwitchBoxesLayoutForm_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				WindowHelper.SendMessageToWndRevit();
				if (this.m_InfoChanged)
				{
					this.PressESC();
					this.m_bKeyEsc = false;
					return;
				}
			}
			else
			{
				base.Focus();
			}
		}

		private List<Curve> CreateCrossCvs(XYZ ptBase)
		{
			List<Curve> list = new List<Curve>();
			double num = AssistFunc.mmToFeet(25.0);
			XYZ xyz = ptBase - XYZ.BasisX * num * 0.5 + XYZ.BasisY * num * 0.5;
			XYZ xyz2 = xyz - XYZ.BasisY * num;
			XYZ xyz3 = xyz2 + XYZ.BasisX * num;
			XYZ xyz4 = xyz3 + XYZ.BasisY * num;
			Curve item = Line.CreateBound(xyz, xyz3);
			list.Add(item);
			Curve item2 = Line.CreateBound(xyz2, xyz4);
			list.Add(item2);
			return list;
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void PressESC()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			SwitchBoxesLayoutForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			SwitchBoxesLayoutForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void Previewer_Paint(object sender, PaintEventArgs e)
		{
			Graphics graphics = e.Graphics;
            graphics.Clear(System.Drawing.Color.Black);
			this.RefreshPreview(graphics);
		}

		private void RefreshPreview(Graphics g)
		{
			try
			{
				List<CurveInfo> list = new List<CurveInfo>();
				List<TextNoteInfo> textNotes = new List<TextNoteInfo>();
				XYZ zero = XYZ.Zero;
				XYZ xyz = XYZ.BasisX;
				XYZ xyz2 = XYZ.BasisY;
				double num = (double)this.m_nPlanAng * Math.PI * 0.5;
				Transform transform = Transform.CreateRotationAtPoint(XYZ.BasisZ, num, zero);
				xyz = transform.OfVector(xyz);
				xyz2 = transform.OfVector(xyz2);
				double num2 = AssistFunc.mmToFeet(200.0);
				double num3 = AssistFunc.mmToFeet(100.0);
				XYZ xyz3 = zero;
				for (int i = 0; i < 3; i++)
				{
					XYZ xyz4 = xyz3 - xyz * num2 * 0.5;
					xyz4 += xyz2 * num3 * 0.5;
					XYZ xyz5 = xyz4 - xyz2 * num3;
					XYZ xyz6 = xyz5 + xyz * num2;
					XYZ xyz7 = xyz6 + xyz2 * num3;
					Curve curve = Line.CreateBound(xyz4, xyz5);
					Curve curve2 = Line.CreateBound(xyz5, xyz6);
					Curve curve3 = Line.CreateBound(xyz6, xyz7);
					Curve curve4 = Line.CreateBound(xyz7, xyz4);
					list.Add(new CurveInfo(curve, PenColor.PC_Black, 0f));
					list.Add(new CurveInfo(curve2, PenColor.PC_Green, 3f));
					list.Add(new CurveInfo(curve3, PenColor.PC_Black, 0f));
					list.Add(new CurveInfo(curve4, PenColor.PC_Black, 0f));
					xyz3 += xyz * num2;
				}
				List<XYZ> list2 = new List<XYZ>();
				XYZ xyz8 = zero - xyz * num2 * 0.5;
				list2.Add(xyz8 + xyz2 * num3 * 0.5);
				list2.Add(xyz8);
				list2.Add(xyz8 - xyz2 * num3 * 0.5);
				XYZ xyz9 = xyz8 + xyz * 3.0 * num2;
				list2.Add(xyz9 - xyz2 * num3 * 0.5);
				list2.Add(xyz9);
				list2.Add(xyz9 + xyz2 * num3 * 0.5);
				for (int j = 0; j < 6; j++)
				{
					XYZ ptBase = list2[j];
					PenColor color = (j == (int)this.m_nInsPos) ? PenColor.PC_Red : PenColor.PC_Blue;
					foreach (Curve curve5 in this.CreateCrossCvs(ptBase))
					{
						list.Add(new CurveInfo(curve5, color, 0f));
					}
				}
				this.m_MainPreviewer.Curves = list;
				this.m_MainPreviewer.TextNotes = textNotes;
			}
			catch (Exception)
			{
			}
		}

		public int Count
		{
			get
			{
				return Convert.ToInt32(this.txtNum.Text.Trim());
			}
		}

		public double OffsetX
		{
			get
			{
				return Convert.ToDouble(this.txtXOffset.Text.Trim());
			}
		}

		public double OffsetY
		{
			get
			{
				return Convert.ToDouble(this.txtYOffset.Text.Trim());
			}
		}

		public bool IsDim
		{
			get
			{
				return this.ckbDisplayNum.Checked;
			}
		}

		public string Prefix
		{
			get
			{
				return this.txtHeadNum.Text;
			}
		}

		public int StartNum
		{
			get
			{
				return Convert.ToInt32(this.txtStartNum.Text.Trim());
			}
		}

		public SwitchBoxesLayoutForm.ELabelSortType SortType
		{
			get
			{
				return (SwitchBoxesLayoutForm.ELabelSortType)this.cmbArrangementStyle.SelectedIndex;
			}
		}

		public SwitchBoxesLayoutForm.EInsPos InsPos
		{
			get
			{
				return this.m_nInsPos;
			}
		}

		public SwitchBoxesLayoutForm.EPlanAng PlanAng
		{
			get
			{
				return this.m_nPlanAng;
			}
		}

		public bool InfoChanged
		{
			get
			{
				return this.m_InfoChanged;
			}
			set
			{
				this.m_InfoChanged = value;
			}
		}

		public bool IsKeyboardEsc
		{
			get
			{
				return this.m_bKeyEsc;
			}
			set
			{
				this.m_bKeyEsc = value;
			}
		}

		private const string LocationX = "X";

		private const string LocationY = "Y";

		private SwitchBoxesLayoutForm.EInsPos m_nInsPos;

		private SwitchBoxesLayoutForm.EPlanAng m_nPlanAng;

		private bool m_InfoChanged;

		private bool m_bKeyEsc = true;

		private bool m_IsInitialized;

		private Previewer m_MainPreviewer;

		private const int m_iPreviewIdx = 1;

		public enum ELabelSortType
		{
			eSortAsc,
			eSortDsc
		}

		public enum EInsPos
		{
			eInsPosLeftUp,
			eInsPosLeftMid,
			eInsPosLeftDown,
			eInsPosRightDown,
			eInsPosRightMid,
			eInsPosRightUp,
			eInsPosInvalid
		}

		public enum EPlanAng
		{
			ePlanAng0,
			ePlanAng90,
			ePlanAng180,
			ePlanAng270,
			ePlanAngInvalid
		}
	}
}
